import { weixinShare } from '../service/getDate'
import { Toast } from 'mint-ui'

// 微信分享功能
var wx = require('weixin-js-sdk')
var shareTitle = '为公行益 携手共进'
var shareDesc = '为我的方案投票，携手关注特殊儿童-2017 LINGNAN MBA ORIENTATION公益案例策划大赛'
var shareImg = 'https://www.oolagongyi.com/img/logo.jpeg'
// console.log('wx: ', wx)
var appId = ''

export default function WeixinShareWay (shareLink) {
  // 调用后端接口，配置以下信息
  weixinShare().then(res => {
    // console.log('this.weixinConfig: ', res)
    appId = res.appId
    wx.config({
      debug: false,
      appId: res.appId,
      timestamp: res.timestamp,
      nonceStr: res.noncestr,
      signature: res.signature,
      jsApiList: [
        'checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'hideMenuItems',
        'showMenuItems',
        'hideAllNonBaseMenuItem',
        'showAllNonBaseMenuItem',
        'translateVoice',
        'startRecord',
        'stopRecord',
        'onRecordEnd',
        'playVoice',
        'pauseVoice',
        'stopVoice',
        'uploadVoice',
        'downloadVoice',
        'chooseImage',
        'previewImage',
        'uploadImage',
        'downloadImage',
        'getNetworkType',
        'openLocation',
        'getLocation',
        'hideOptionMenu',
        'showOptionMenu',
        'closeWindow',
        'scanQRCode',
        'chooseWXPay',
        'openProductSpecificView',
        'addCard',
        'chooseCard',
        'openCard'
      ]
    })
    wx.checkJsApi({
      jsApiList: [
        'checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'hideMenuItems',
        'showMenuItems',
        'hideAllNonBaseMenuItem',
        'showAllNonBaseMenuItem',
        'translateVoice',
        'startRecord',
        'stopRecord',
        'onRecordEnd',
        'playVoice',
        'pauseVoice',
        'stopVoice',
        'uploadVoice',
        'downloadVoice',
        'chooseImage',
        'previewImage',
        'uploadImage',
        'downloadImage',
        'getNetworkType',
        'openLocation',
        'getLocation',
        'hideOptionMenu',
        'showOptionMenu',
        'closeWindow',
        'scanQRCode',
        'chooseWXPay',
        'openProductSpecificView',
        'addCard',
        'chooseCard',
        'openCard'
      ],
      success: function (res) {
        // console.log('checkJsApi:', res)
      }
    })
    wx.ready(function () {
      window.DS.ready(function () {
        window.DS.sendAuthUserInfo({}, appId)

        wx.onMenuShareAppMessage({
          title: shareTitle,
          desc: shareDesc,
          link: shareLink,
          imgUrl: shareImg,
          success: function () {
            Toast('分享成功啦')
            window.DS.sendRepost('appMessage')
          },
          cancel: function () {
            Toast('分享失败')
          }
        })
        wx.onMenuShareTimeline({
          title: shareTitle,
          desc: shareDesc,
          link: shareLink,
          imgUrl: shareImg,
          success: function () {
            Toast('分享成功啦')
            window.DS.sendRepost('timeline')
          },
          cancel: function () {
            Toast('分享失败')
          }
        })
      })
    })
    wx.error(function (res) {
      // console.log('error')
      Toast(res)
    })
  })
}
